package trombinoscope.servlet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import trombinoscope.modele.EtudiantDAO;
import trombinoscope.modele.Etudiant;
/**
 *
 * @author jef
 */
@WebServlet(name = "ServletSupprimer2", urlPatterns = {"/ServletSupprimer2"})
public class ServletSupprimer2 extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            response.setContentType("text/html");
            
        
            boolean statut = (Boolean)getServletContext().getAttribute("adminUser"); // récupération du statut
           
            String idEtudiant = request.getParameter("idetudiant");
            Integer idEtudiantInt = Integer.parseInt(idEtudiant);
            
            Etudiant etudiantConcerne = EtudiantDAO.getEtudiantByIdEtudiant(idEtudiantInt);
           
            // récupération des valeurs du formulaire de suppression de la fiche
           /* 
            String nom = request.getParameter("nomEtudiant");
            String prenom = request.getParameter("prenomEtudiant");
            String dateNaissance = request.getParameter("dateNaissance");
            String adresse = request.getParameter("adresse");
            String telDomicile = request.getParameter("numDomicile");
            String telPortable = request.getParameter("numPortable");
            String mail = request.getParameter("mail");
            
            if(Etudiant.isEtudiantExistant(idEtudiantInt, nom, prenom, adresse, telDomicile, telPortable, mail) == false)
            {
		out.println("<script type='text/javascript'> alert('L'étudiant à supprimer n'existe pas !') </script>");
                out.println("<script type='text/javascript'> document.location.href='./ficheEtudiant.jsp' </script>");
            }
            
            else
            {
                
                etudiantConcerne.setNom(nom);
                etudiantConcerne.setPrenom(prenom);
                etudiantConcerne.setDate_naissance(dateNaissance);
                etudiantConcerne.setAdresse(adresse);
                etudiantConcerne.setNumTelDomicile(telDomicile);
                etudiantConcerne.setNumTelPortable(telPortable);
                etudiantConcerne.setEmail(mail);
            
                etudiantConcerne.save(); 
            
            */
                Connection con;                  
                Class.forName("com.mysql.jdbc.Driver"); 
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/trombinoscope","root","");
                Statement stm = con.createStatement();
                
                stm.executeUpdate("DELETE FROM etudiant WHERE idUser = '"+idEtudiant+"'"); 
                stm.executeUpdate("DELETE FROM utilisateur WHERE idUser = '"+idEtudiant+"'"); 
                
                out.println("<script type='text/javascript'> alert('La fiche a bien été supprimé.') </script>");
                out.println("<script type='text/javascript'> document.location.href='./accueil.jsp' </script>");
               
               // récupèration du contexte de l'application
                ServletContext context = getServletContext();
                    
                context.setAttribute("etudiant", etudiantConcerne);
                context.setAttribute("isAdminUser", statut);
                     
                //request.getRequestDispatcher("ficheModifEtudiant.jsp").forward(request, response);
                    
            
            //}
            
        } 
        
        catch (SQLException e) 
        {	
            response.setContentType("text/html");
            
            out.println("<p><strong> Erreur avec la base. </strong>");
            out.println("<p> <a href= \"./trombiListeEtudiants.jsp \"></a> </p>");
            out.println("Cause de l'erreur : " + e.getCause());
		
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}


            /*
            String idUser = request.getParameter("idUser");
            String droitAcces = request.getParameter("droitAcces");
            String photo = request.getParameter("photo");
            String description = request.getParameter("description");
            
            
          
                 
            out.println("<html>");
            out.println("<head>");
            out.println("<title>ServletSupprimer</title>");  
            out.println("</head>");
            out.println("<body style='background-color: rgb(255, 255, 128);'>");
            out.println("<h1>Servlet de Suppression : </h1>");
            out.println ("Vous venez de supprimer l'élément suivant : " + "<br>");
            out.println ("id : "  + idUser + "<br>");
            out.println ("droitAcces : " + droitAcces + "<br>");
            out.println ("photo : "   + photo + "<br>"); 
            out.println ("description : " + description + "<br>");   
             
            
            out.println("<br><br>");
            out.println("</body>");
            out.println("</html>");
               
            Connection con;
                 
                Class.forName("com.mysql.jdbc.Driver"); 
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/trombinoscope","root","");

               
                 Statement stm = con.createStatement();
                
             
                   out.print("<table border='1' align='center' bgcolor='blue'>");
		  out.print ("<tr  valign='middle' align='center'>");
		  out.print ("<td width='150'> 'id'  </td>");
                  out.print ("<td width='150'> 'droitAcces'  </td>");
		  out.print ("<td width='150'> 'photo'  </td>");		
	          out.print ("<td width='150'> 'description'  </td>");	
                  out.print ("</tr>");	
		  out.print ("</table>");

                   String requete1 =   "SELECT * FROM etudiant WHERE idUser = '"+idUser+"' AND droitAcces = '"+droitAcces+"' AND photo = '"+ photo +"' AND description = '"+ description +"'";         
                 ResultSet r= stm.executeQuery(requete1); 
                 while (r.next()){
                  out.print("<table border='1' align='center' bgcolor='white'>");
		  out.print ("<tr  valign='middle' align='center'>");
		  out.print ("<td width='150'> "+r.getString(1)+"  </td>");
		  out.print ("<td width='150'> "+r.getString(2)+"  </td>");		
	          out.print ("<td width='150'> "+r.getString(3)+"  </td>");	
                  out.print ("<td width='150'> "+r.getString(4)+"  </td>");	
		  out.print ("</tr>");	
		  out.print ("</table>");
                }
                 
                 out.println ("<center>");
                 out.println("<button><a href='accueil.jsp'> Valider </a></button>");  
                 out.println("</center> ");
                 out.println ("<p align='center' size='26'> "+  "Voici la nouvelle base de données : " + "</p>");
 
             
              String requete2 = "DELETE FROM etudiant WHERE idUser = '"+idUser+"' AND droitAcces = '"+droitAcces+"' AND photo = '"+ photo +"' AND description = '"+ description +"'";
              stm.executeUpdate(requete2); 
               
            
                
               out.print("<table border='1' align='center' bgcolor='blue'>");
		  out.print ("<tr  valign='middle' align='center'>");
		  out.print ("<td width='150'> 'id'  </td>");
                  out.print ("<td width='150'> 'nom'  </td>");
                  out.print ("<td width='150'> 'age'  </td>");
		  out.print ("<td width='150'> 'poids'  </td>");                 
		  out.print ("</tr>");	
		  out.print ("</table>");
              
               
                ResultSet res=stm.executeQuery("SELECT * FROM etudiant ORDER BY idUser ASC");
                while (res.next()){
                  	
                  out.print("<table border='1' align='center' bgcolor='white'>");
		  out.print ("<tr  valign='middle' align='center'>");
		  out.print ("<td width='150'> "+res.getString(1)+"  </td>");
		  out.print ("<td width='150'> "+res.getString(2)+"  </td>");		
	          out.print ("<td width='150'> "+res.getString(3)+"  </td>");	
                  out.print ("<td width='150'> "+res.getString(4)+"  </td>");	
		  out.print ("</tr>");	
		  out.print ("</table>");
                   // out.println("<p align='center'> age :   \t"+res.getInt(1)+"   poids :   \t"+res.getInt(2) + "   nom :    \t" + res.getString(3)+ "   bouh :    \t" + res.getString(4)+ "</p>");
                }
                 
                con.close(); 
               
                
               */ 
         
     


